#include "nnslitetest.h"


static f64 nnslitetest_1_distfunc(const nnsliteObject* object1, const nnsliteObject* object2)
{
	s32 f1 = *(s32*)(object1->data);
	s32 f2 = *(s32*)(object2->data);
	return (f64)abs(f1-f2);
}

static nnsliteObject nnslitetest_1_createObj(s32 value, u64 id)
{
	nnsliteObject obj;
	nnsliteObjectInit(&obj);

	obj.size = sizeof(s32);
	obj.data = malloc(sizeof(s32));
	*(s32*)obj.data = value;
	obj.id = id;
	return obj;
}

NNSLITE_RET nnslitetest_1()
{
	s32 ret;
	nnsliteStore store;
	nnsliteObject obj1;
	nnsliteObject obj2[5];
	FILE* fp;
	u32 i;

	fp = fopen("nnslitetest_1.txt","w");

	ret = nnsliteStoreCreate(&store, "nnslitetest_1", nnslitetest_1_distfunc);
	if(NNSLITE_FAILED(ret))
	{
		printf("Cannot create the store : nnslitetest_1 \n");
		return -1;
	}

	for (i=0;i<32;i++)
	{
		obj1 = nnslitetest_1_createObj(100+i, 100+i);
		nnsliteStoreInsert(&store, &obj1);
		nnsliteObjectFree(&obj1);
		nnsliteStorePrint(&store,fp);
		fprintf(fp,"================time %d=================\n",i);
		fflush(fp);
	}

	nnsliteStorePrint(&store,fp);

	for (i=0;i<5;i++)
		nnsliteObjectInit(&obj2[i]);

	obj1 = nnslitetest_1_createObj(107, 107);
	nnsliteStoreNNSearch(&store, &obj1, 5, obj2);
	nnsliteObjectFree(&obj1);

	for (i=0;i<5;i++)
	{
		fprintf(fp,"%ld ",obj2[i].id);
		nnsliteObjectFree(&obj2[i]);
	}
	fprintf(fp,"\n");
	

	nnsliteStoreFree(&store);

	fclose(fp);
	return NNSLITE_RET_OK;
}
